import java.awt.event.*;
import java.awt.Rectangle;

import java.awt.Toolkit;
import java.awt.geom.*;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.ImageIcon;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;




public class Board extends JPanel implements ActionListener
{
	private Timer timer;
	private Player player;
	private World world;
	private final int RENDER_HEIGHT = 25, RENDER_WIDTH = 25;
	private Block facing;

	private Block[] trees = new Block[World.NUM_TREES];
	private ArrayList<Ellipse2D.Double> circles = new ArrayList<Ellipse2D.Double>();
	private double time = 0;
	private boolean isDone = false;
	private Image imgStump = new ImageIcon(this.getClass().getResource("images/stump.png")).getImage();
	private int NUM_SCORES = 10;
	private ArrayList<Double> scores = new ArrayList<Double>();
	private ArrayList<String> scoreNames = new ArrayList<String>();
	
	public Board()
	{
		world = new World();
		player = new Player(12 * 15, 24 * 15);
		facing = new Block(0, 0);
		
        setFocusable(true);
        addKeyListener(new TAdapter());
        setDoubleBuffered(true);
        
        trees = world.getTrees();
        
       
        timer = new Timer(25, this);
        timer.start();
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		Graphics2D g2 = (Graphics2D)g;
		
		
		//reset circles
		for(int i = 0; i < circles.size();)
			circles.remove(0);
		
		for(Block e : trees)
		{
			if(e.getImage() != imgStump)
				circles.add(new Ellipse2D.Double((e.getI() - 1) * 15, (e.getJ() - 1) * 15, 45, 45));
		}
	        
		renderWorld(g2);
		
		for(Ellipse2D.Double e : circles)
		{
			g2.draw(e);
		}
		
		g2.drawString("Time: " + (int)(time/10) / 100.0   + "seconds", 10, 10);
		
		if(circles.isEmpty())//if they win
		{
			timer.stop();
			double timeToWrite = (int)(time / 10) / 100.0;
			
		
			System.out.println("Please enter your name: ");
			Scanner in = new Scanner(System.in);
			String name = in.next();
			scores.add(timeToWrite);
			scoreNames.add(name);
			
			try{
			PrintWriter out = new PrintWriter("scores.txt");
			for(int i = 0; i < scores.size(); i++)
			{
				out.write(name + " " + timeToWrite);
				System.out.println(scoreNames.get(i) + " " + scores.get(i)); }
			}
			catch(FileNotFoundException e)
			{
				e.printStackTrace();
			}
			
		}
		
		g2.drawImage(player.getImage(), player.getX(), player.getY(), this);
		//g2.draw(player.getBounds());
		g2.draw(facing.getBounds(world.getX(), world.getY()));
	}
	
	public void renderWorld(Graphics2D g2)
	{
		
		for(int i = 0; i < RENDER_WIDTH; i++)
			for(int j = 0; j < RENDER_HEIGHT; j++)
			{
				g2.drawImage(world.getImageToRenderAt(j, i), j * 15, i * 15, this);
				//g2.draw(world.getBlockAt(j, i).getBounds());
			}
		
		
	}
	
	
	public void actionPerformed(ActionEvent ae)
	{
		if(!isDone)
			time += 25;
		
		player.move();
		updateFacing();
		repaint();
	}
	
	
	public void updateFacing()
	{
		int playerX = player.getXLocation(world.getX());
		int playerY = player.getYLocation(world.getY());
		int facingX = 0;
		int facingY = 0;
		int direction = player.getDirection();
		
		if(direction == 0 || direction == 4) //if 1 or 4
			facingX = playerX;
		else if(direction >= 1 && direction <= 3) //if between 1 and 3 inc
			facingX = playerX + 1;
		else if(direction >= 5 && direction <= 7) //if between 5 and 7 inc
			facingX = playerX - 1;
		
		if(direction == 2 || direction == 6)
			facingY = playerY;
		else if(direction >= 3 && direction <= 5)
			facingY = playerY + 1;
		else if(direction == 0 || direction == 1 || direction == 7)
			facingY = playerY - 1;
		
		facing = world.getBlockAt(facingX + world.getX(), facingY + world.getY());
	}
	
	private void loadScore()
	{
		String[] chicken = new String[NUM_SCORES];
		
		try
		{
			Scanner in = new Scanner(new File("scores.txt"));
			
			int i = 0;
			while(in.hasNextLine())
			{
				chicken[i] = in.nextLine();
				i++;
			}
		}
		catch(FileNotFoundException e)
		{
			e.printStackTrace();
		}
		
		for(int i = 0; i < NUM_SCORES; i++)
		{
			int from, to;
			
			from = 0;
			to = chicken[i].indexOf(' ');
			scoreNames.add(chicken[i].substring(from, to));
			
			chicken[i].replace(" ", "x");
			
			from = chicken[i].indexOf('x') + 1;
			to = chicken[i].indexOf(' ');
			scores.add(Double.valueOf(chicken[i].substring(from, to)));
	
		}
	}
			
		
	private class TAdapter extends KeyAdapter
	{
	    public void keyReleased(KeyEvent e)
	    {
	    	
	    	player.keyReleased(e);
	    	
	    }

	    public void keyPressed(KeyEvent e)
	    {
	    	if(e.getKeyCode() != KeyEvent.VK_SPACE)
	    		player.keyPressed(e);
	    	else //if spacebar was pressed
	    		facing.hit();
	    }
	}
}
